package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;
import cn.itcast.service.OrderSettingsService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportIncomeController {


    @Reference(version = "1.0.0")
    private OrderSettingsService orderSettingsService;

    /**
     * 预约/支付统计
     * @return
     */
    @GetMapping("/income")
    public Result reportIncomeCount(){
        List<String> months=new ArrayList<>();
        //获取当前时间
        Calendar calendar = Calendar.getInstance();
        //获取去年时间
        calendar.add(Calendar.YEAR,-1);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
        for (int i = 0; i < 12; i++) {
            //去年月份每次加一
            calendar.add(Calendar.MONTH,1);
            Date date = calendar.getTime();
            //添加到月份集合中
            months.add(sdf.format(date));
        }

        //创建存储每月收入和的集合
        List<Integer> counts=new ArrayList<>();
        //获取到了去年一年的每一个年月
        //遍历年月份集合
        for (String month : months) {
            //获取每月收入总和
            Integer count = this.orderSettingsService.queryOrderSettingsSum(month + "-1", month + "-31");
            counts.add(count);
        }

        Map<String,Object> map=new HashMap<>();
        //设置返回前端值
        map.put("setmealDates",months);
        map.put("setmealCounts",counts);
        return new Result(true, MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);

    }
}
